import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzq.SmileClientApplication;
import com.zzq.domain.entity.SmileCheckRecord;
import com.zzq.enumerate.CheckDayStatusEnum;
import com.zzq.service.SmileCheckRecordService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;
@Service
@Slf4j
@SpringBootTest(classes = SmileClientApplication.class)
public class ClientTask {
    @Autowired
    SmileCheckRecordService checkRecordService;

    @Test

    public void CheckDayTask(){

        log.info("更新签到信息开始");
        LambdaQueryWrapper<SmileCheckRecord> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SmileCheckRecord::getCheckDayStatus, CheckDayStatusEnum.TODAY.getName()).or()
                .eq(SmileCheckRecord::getCheckDayStatus, CheckDayStatusEnum.YESTERDAY.getName());
        List<SmileCheckRecord> checkRecordList = checkRecordService.list(queryWrapper);
        checkRecordList.stream().forEach(smileCheckRecord -> {
            if (CheckDayStatusEnum.TODAY.getName().equals(smileCheckRecord.getCheckDayStatus())) {
                smileCheckRecord.setCheckDayStatus(CheckDayStatusEnum.YESTERDAY.getName());
                checkRecordService.updateById(smileCheckRecord);
            } else if (CheckDayStatusEnum.YESTERDAY.getName().equals(smileCheckRecord.getCheckDayStatus())) {
                smileCheckRecord.setCheckDayStatus(CheckDayStatusEnum.PASTDAY.getName());
                checkRecordService.updateById(smileCheckRecord);
            }
            log.info("正在更新签到：用户{}",smileCheckRecord.getClientNo());
        });

        log.info("更新签到信息结束");
    }


    public static boolean isWithinDays(Date baseDate, Date targetDate, int days) {
        long diff = targetDate.getTime() - baseDate.getTime();
        long dayDifference = diff / (24 * 60 * 60 * 1000);
        return dayDifference >= 0 && dayDifference <= days;
    }

    public static void main(String[] args) {
        Date baseDate = java.sql.Date.valueOf("2023-01-09");
        Date targetDate = java.sql.Date.valueOf("2023-01-10");

        System.out.println(isWithinDays(baseDate, targetDate, 7));
    }



}
